<?php

	namespace model;

		class SessionModel
		{

			//private statics
			private static $sessionLocation = "thisSession";
			private static $cookieName = "Name";
			private static $cookiePassword = "Password";
			private static $cookieExpireTime = "ExpireTime";
			private static $sessionPassword = "Session_Password";
			private static $sessionName = "Session_Name";
			private static $sessionIp = "SessionIp";
			private static $sessionBrowser = "SessionBrowser";
			private static $cookieExpireTimeFile = "cookieExpireTime.txt";
			//strings
			private $session;
			private $userName;
			private $tempCorrectPassW;
			private $message = "";
			private $returningName;
			private $status = "Ej Inloggad!";
			// bool
			private $isUserLoggedInWithSession;
			//Time
			private $expireTime;
			//Array
			private $validateCookieData;

			//Returns string message
			public function getsessionModelMessage()
			{
				return $this->message;
			}


			//Returns string status
			public function getsessionModelStatus()
			{
				return $this->status;
			}


			//return bool (isset)
			public function isSessionSet()
			{
				return isset($_SESSION[self::$sessionLocation]);
			}


			public function isUserLoggedInWithCookies()
			{
				return $this->isUserLoggedInWithSession;
			}


			public function validateOngoingLogin()
			{
				//If session exists
				if (isset($_SESSION[self::$sessionLocation]))
				{
					return $this->message = $this->validateSession();
				}

				//If cookies are set
				if (isset($_COOKIE[self::$cookieName]) 
				&& isset($_COOKIE[self::$cookiePassword]))
				{
					return $this->message = $this->validateSession();
				}

				//in no session exists
				else if (!isset($_SESSION[self::$sessionLocation]))
				{
					$this->isUserLoggedInWithSession = false;
					return $this->message = "Ingen session här!";
				}

				else
				{
					$this->isUserLoggedInWithSession = false;
					return $this->message = "Du är inte inloggad på något sätt";
				}
			}


			public function validateSession()
			{
				//If user is logged in with cookie TODO - Validate cookies
				if (isset($_COOKIE[self::$cookieName]) && isset($_COOKIE[self::$cookiePassword]))
				{
					$this->validateCookieData = unserialize(file_get_contents(self::$cookieExpireTimeFile));
					if (($_COOKIE[self::$cookieName]) == $this->validateCookieData[self::$cookieName] && ($_COOKIE[self::$cookiePassword]) == $this->validateCookieData[self::$cookiePassword] && time() < $this->validateCookieData[self::$cookieExpireTime])

					{
						$this->status = "Inloggad";
						$this->isUserLoggedInWithSession = true;
						return $this->message = "Du är inloggad med hjälp av kakor " . $_COOKIE[self::$cookieName];
					}

					else
					{
						//TODO throw new \Exception("KAKTRAMS");
						setcookie(self::$cookieName, "", time() - 3600);
						setcookie(self::$cookiePassword, "", time() - 3600);

						$this->isUserLoggedInWithSession = false;
						return $this->message = "TRAMSA INTE MED KAKORNA!";
					}
				}

				if (!isset($_SESSION[self::$sessionLocation]))
				{
					$this->setUserSession();
				}
				
				else if (isset($_SESSION[self::$sessionLocation]))
				{
					//If browswer or ip address are incorrect
					if ($_SESSION[self::$sessionLocation][self::$sessionBrowser] != $_SERVER['HTTP_USER_AGENT'])
					{
						$this->isUserLoggedInWithSession = false;
						return $this->message = "Tramsa inte med kakorna i olika browsers!";
					}

					if ($_SESSION[self::$sessionLocation][self::$sessionIp] != $_SERVER['REMOTE_ADDR'])
					{
						$this->isUserLoggedInWithSession = false;
						return $this->message = "Tramsa inte med kakornas ip-adress!";
					}

					//If user is logged in with session
					if (isset($_SESSION[self::$sessionLocation][self::$sessionPassword]) && isset($_SESSION[self::$sessionLocation][self::$sessionName]))
					{
						$this->status = "Inloggad";
						$this->isUserLoggedInWithSession = true;
						return $this->message = "Du är inloggad via sessionen " . $_SESSION[self::$sessionLocation][self::$sessionName];
					}
				}
				else
				{
					$this->isUserLoggedInWithSession = false;
					return $this->message = "Vad hände nu?!";
				}
			}


			public function setCookieInfo($validatedUserName, $tempCorrectPassW)
			{
				$this->status = "Inloggad";
				$this->message = "Hej och Välkommen. Vi kommer att komma ihåg dig " . $validatedUserName;

				$this->tempCorrectPassW = $tempCorrectPassW;
				$this->userName = $validatedUserName;
				$this->expireTime = time() + 50;

				setcookie(self::$cookieName, $this->userName, $this->expireTime);
				setcookie(self::$cookiePassword, $this->tempCorrectPassW, $this->expireTime);

				$this->validateCookieData = array(
					self::$cookieName => $this->userName,
					self::$cookiePassword => $this->tempCorrectPassW,
					self::$cookieExpireTime => $this->expireTime
				);

				file_put_contents(self::$cookieExpireTimeFile, serialize($this->validateCookieData));

			}



			public function logOutFromSession()
			{
				//Saves name for later return
				// if (isset($_SESSION[self::$sessionLocation][self::$sessionName]))
				// {
					// $this->returningName = $_SESSION[self::$sessionLocation][self::$sessionName];
				// }
				// else
				// {
					// $this->returningName = null;
				// }

				$this->message = "Du har nu loggat ut, Välkommen åter!";
				$this->isUserLoggedInWithSession = false;
				//Unsets old cookies (if existing)
				//unset($_SESSION[self::$sessionLocation][self::$sessionName], $_SESSION[self::$sessionLocation][self::$sessionPassword]);
				unset($_SESSION[self::$sessionLocation][self::$sessionPassword]);
				session_regenerate_id();
				setcookie(self::$cookieName, "", time() - 3600);
				setcookie(self::$cookiePassword, "", time() - 3600);
				return;

			}


			//@param String $userName, @param String $userPassW
			public function setSession($userName, $userPassW)
			{
				$this->status = "Inloggad";
				$this->message = "Hej och Välkommen. Du är inloggad som " . $userName;
				$this->isUserLoggedInWithSession = true;
				$_SESSION[self::$sessionLocation] = array();
				$_SESSION[self::$sessionLocation][self::$sessionBrowser] = $_SERVER['HTTP_USER_AGENT'];
				$_SESSION[self::$sessionLocation][self::$sessionIp] = $_SERVER['REMOTE_ADDR'];
				$_SESSION[self::$sessionLocation][self::$sessionName] = $userName;
				$_SESSION[self::$sessionLocation][self::$sessionPassword] = $userPassW;
			}


			public function setUserSession()
			{
				$_SESSION[self::$sessionLocation] = array();
				$_SESSION[self::$sessionLocation][self::$sessionBrowser] = $_SERVER['HTTP_USER_AGENT'];
				$_SESSION[self::$sessionLocation][self::$sessionIp] = $_SERVER['REMOTE_ADDR'];
			}


		}
